﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Mvc;

namespace CoolCode.Web.Mvc.UI.Grid
{
	public class ToolbarRenderer : IToolbarRenderer
	{
		public void Render(IToolbarModel Model, TextWriter output, ViewContext viewContext)
		{
			output.Write("<div class=\"tDiv\">");
			output.Write("<div class=\"tDiv2\">");

			foreach (var button in Model.Buttons)
			{
				var b = ((ToolbarButton)button);

				renderToolbarButton(b, output, viewContext);
			}

			output.Write("</div>");
			output.Write("<div style=\"clear: both;\">");
			output.Write("</div>");
			output.Write("</div>");
		}

		private void renderToolbarButton(ToolbarButton b, TextWriter output, ViewContext viewContext)
		{
			if (!string.IsNullOrEmpty(b.Html))
			{
				output.Write(b.Html);
			}
			if (!string.IsNullOrEmpty(b.PartialViewName))
			{
				output.Partial(viewContext, b.PartialViewName);
			}
			else
			{
				output.Write("<div class=\"fbutton\">");
				output.Write("<div>");
				output.Write(string.Format("<span class=\"{0}\" onclick=\"{2}\">{1}</span>", b.Css, b.Name, b.ClickHandler()));
				output.Write("</div>");
				output.Write("</div>");
			}
		}
	}
}
